// JavaScript Document
var product_error=new Array();
function setWrongOrNot(str,bool){
	var isWrongOrNot={
		str:'',
		bool:false
	};
	if(bool){
		isWrongOrNot.str='<span style="color:green;"><img src="html/images/valid/right.gif" style="vertical-align:middle" /> '+str+'</span>';
	}else{
		var shouldPush=true;
		for(var i in product_error){
			if(product_error[i]==str){
				shouldPush=false;
				break;	
			}
		}
		if(shouldPush){
			product_error.push(str);
		}
		isWrongOrNot.str='<span style="color:red;"><img src="html/images/valid/wrong.gif" style="vertical-align:middle" /> '+str+'</span>';	
	}
	isWrongOrNot.bool=bool;
	return isWrongOrNot;
}
function validateProduct(){
	this.checkProductName=function(str){
		//var re=/^[a-zA-Z0-9\s,\.'";!:\-\(\)\+\*#]+$/;
		if(trim(str)==''){
			return setWrongOrNot('产品名称只能带有是英文,数字和空格!',false);
		}else{	
			return setWrongOrNot('填写正确!',true);
		}
	},
	this.checkPattern=function(str){
		if(trim(str)==''){
			return setWrongOrNot('请填写好产品规格!',false);	
		}else{
			return setWrongOrNot('填写正确!',true);	
		}
	},
	this.checkFenlei=function(str){
		if(str==0||!numTest(str)){
			return setWrongOrNot('请选择分类!',false);	
		}else{
			return setWrongOrNot('选择正确!',true);	
		}
	},
	this.checkLocation=function(str){
		if(str==0||!numTest(str)){
			return setWrongOrNot('请选择产品地区!',false);	
		}else{
			return setWrongOrNot('选择正确!',true);	
		}
	}
	this.checkFCK=function(){
		var content = FCKeditorAPI.GetInstance('FCKeditor1').GetXHTML( true );
		if(trim(content)==''){
			return setWrongOrNot('请填写好产品描述!',false);
		}else{
			return setWrongOrNot('填写正确!',true);	
		}
	},
	this.checkPackCounts=function(str){
		if(!numTest(str)||trim(str)==''){
			return setWrongOrNot('您输入的每包产品数量有误!',false);	
		}else{
			return setWrongOrNot('填写正确!',true);	
		}			
	},
	this.checkSize=function(str){
		var re=/^[0-9\.]+$/;
		if(!re.test(str)||trim(str)==''){
			return setWrongOrNot('您输入的尺寸有误!',false);
		}else{	
			return setWrongOrNot('填写正确!',true);
		}
	},
	this.checkWeight=function(str){
		var re=/^[0-9\.]+$/;
		if(!re.test(str)||trim(str)==''){
			return setWrongOrNot('您输入的重量有误!',false);
		}else{	
			return setWrongOrNot('填写正确!',true);
		}
	},
	this.checkMinCounts=function(str){
		if(!numTest(str)||trim(str)==''){
			return setWrongOrNot('您输入的起始采购量有误!',false);	
		}else{
			return setWrongOrNot('填写正确!',true);	
		}
	},
	this.checkMaxCounts=function(str){
		if(!numTest(str)||trim(str)==''){
			return setWrongOrNot('您输入的截止采购量有误!',false);	
		}else{
			return setWrongOrNot('填写正确!',true);	
		}			
	},
	this.checkSupplyPrice=function(str){
		var re=/^[0-9\.]+$/;
		if(!re.test(str)||trim(str)==''){
			return setWrongOrNot('您输入的供应商价有误!',false);
		}else{	
			return setWrongOrNot('填写正确!',true);
		}		
	},
	this.checkDeliveryDay=function(str){
		if(!numTest(str)||trim(str)==''){
			return setWrongOrNot('您输入的交货天数有误!',false);	
		}else{
			return setWrongOrNot('填写正确!',true);	
		}
	}
}


function priceCheck(validate){
	$('input[name="min_counts[]"]').unbind();
	$('input[name="min_counts[]"]').keyup(
		function(){
			productCommonCheck.checkMinCounts(validate,$(this));
		}
	);
	$('input[name="min_counts[]"]').blur(
		function(){
			productCommonCheck.checkMinCounts(validate,$(this));
		}
	);
	
	$('input[name="max_counts[]"]').unbind();
	$('input[name="max_counts[]"]').keyup(
		function(){
			productCommonCheck.checkMaxCounts(validate,$(this));
		}							  
	);
	$('input[name="max_counts[]"]').blur(
		function(){
			productCommonCheck.checkMaxCounts(validate,$(this));
		}							  
	);
	
	$('input[name="supply_price[]"]').unbind();
	$('input[name="supply_price[]"]').keyup(
		function(){
			productCommonCheck.checkSupplyPrice(validate,$(this));
			$($(this).parent().next().children().get(0)).val(formatfloat($(this).val()*1,2));	
		}							  
	);
	$('input[name="supply_price[]"]').blur(
		function(){
			productCommonCheck.checkSupplyPrice(validate,$(this));
		}							  
	);	
	
	$('input[name="delivery_days[]"]').unbind();
	$('input[name="delivery_days[]"]').keyup(
		function(){
			productCommonCheck.checkDeliveryDay(validate,$(this));
		}							  
	);	
	$('input[name="delivery_days[]"]').blur(
		function(){
			productCommonCheck.checkDeliveryDay(validate,$(this));
		}							  
	);
}

function packCountCheck(validate){
	$('input[name="counts_per_pack"]').unbind();
	$('input[name="counts_per_pack"]').keyup(
		function(){
			productCommonCheck.checkPackCounts(validate,$(this));
		}							  
	);
	$('input[name="counts_per_pack"]').blur(
		function(){
			productCommonCheck.checkPackCounts(validate,$(this));
		}							  
	);	
}

var productCommonCheck={
	checkProductName:function(validate,thisA){
		$(thisA).parent().next().html(validate.checkProductName($(thisA).val()).str);
	},
	checkPattern:function(validate,thisA){
		$(thisA).parent().next().html(validate.checkPattern($(thisA).val()).str);
	},
	checkFck:function(validate){
		$('#FCKeditor1').parent().parent().next().html(validate.checkFCK().str);	
	},
	checkPackCounts:function(validate,thisA){
		if(!validate.checkPackCounts($(thisA).val()).bool){
			$(thisA).val($(thisA).val().replace(new RegExp("[^0-9]",'g'),''));
		}	
	},
	checkWeight:function(validate,thisA){
		if(!validate.checkWeight($(thisA).val()).bool){
			$(thisA).val($(thisA).val().replace(new RegExp("[^0-9\.]",'g'),''));
		}
	},
	checkSize:function(validate,thisA){
		if(!validate.checkSize($(thisA).val()).bool){
			$(thisA).val($(thisA).val().replace(new RegExp("[^0-9\.]",'g'),''));
		}
	},
	checkMinCounts:function(validate,thisA){
		if(!validate.checkMinCounts($(thisA).val()).bool){
			$(thisA).val($(thisA).val().replace(new RegExp("[^0-9\.]",'g'),''));
		}
	},
	checkMaxCounts:function(validate,thisA){
		if(!validate.checkMaxCounts($(thisA).val()).bool){
			$(thisA).val($(thisA).val().replace(new RegExp("[^0-9\.]",'g'),''));
		}
	},
	checkSupplyPrice:function(validate,thisA){
		if(!validate.checkSupplyPrice($(thisA).val()).bool){
			$(thisA).val($(thisA).val().replace(new RegExp("[^0-9\.]",'g'),''));
		}
	},
	checkDeliveryDay:function(validate,thisA){
		if(!validate.checkDeliveryDay($(thisA).val()).bool){
			$(thisA).val($(thisA).val().replace(new RegExp("[^0-9]",'g'),''));
		}
	},
	checkAll:function(){
		var validate=new validateProduct();
		
		if(!validate.checkFenlei($('#flid').val()).bool){
			alert('请选择分类!');
			$('#flSelect').focus();
			$('#flSelect').css('background','#FFCACA');
			return;
		}
		$('#flSelect').css('background','none');
		/*
		if(!validate.checkLocation($('#location_id').val()).bool){
			alert('请选择产品地区!');
			$('#locationSelect').focus();
			$('#locationSelect').css('background','#FFCACA');
			return;
		}
		$('#locationSelect').css('background','none');
		*/
		if(!validate.checkProductName($('input[name="subject"]').val()).bool){
			$('input[name="subject"]').blur();
			$('input[name="subject"]').css('backgroundColor','#FFCACA');
			$('input[name="subject"]').focus();	
			return;
		}
		$('input[name="subject"]').css('background','none');
		/*
		if(!validate.checkPattern($('textarea[name="pattern_intro"]').val()).bool){
			$('textarea[name="pattern_intro"]').blur();
			$('textarea[name="pattern_intro"]').css('backgroundColor','#FFCACA');
			$('textarea[name="pattern_intro"]').focus();
			return;
		}
		$('textarea[name="pattern_intro"]').css('background','none');
		*/
		if(!validate.checkFCK().bool){
			var fck = FCKeditorAPI.GetInstance('FCKeditor1')
			$('#FCKeditor1').parent().parent().next().html(validate.checkFCK().str);
			fck.Focus();
			return;
		}

		if($('input[name="counts_per_pack"]').size()==1){
			if(!validate.checkPackCounts($('input[name="counts_per_pack"]').val()).bool){
				alert("您输入的每包产品数量有误!");
				$('input[name="counts_per_pack"]').css('background','#FFCACA');
				$('input[name="counts_per_pack"]').focus();
				return;
			}
			$('input[name="counts_per_pack"]').css('background','none');
		}

		if(!validate.checkSize($('input[name="size1"]').val()).bool){
			alert("请检查您产品的大小!");
			$('input[name="size1"]').css('background','#FFCACA');
			$('input[name="size1"]').focus();
			return;
		}
		$('input[name="size1"]').css('background','none');
		if(!validate.checkSize($('input[name="size2"]').val()).bool){
			alert("请检查您产品的大小!");
			$('input[name="size2"]').css('background','#FFCACA');
			$('input[name="size2"]').focus();
			return;
		}
		$('input[name="size2"]').css('background','none');
		if(!validate.checkSize($('input[name="size3"]').val()).bool){
			alert("请检查您产品的大小!");
			$('input[name="size3"]').css('background','#FFCACA');
			$('input[name="size3"]').focus();
			return;
		}
		$('input[name="size3"]').css('background','none');

		if(!validate.checkWeight($('input[name="weight"]').val()).bool){
			alert("请检查您产品的重量!");
			$('input[name="weight"]').css('background','#FFCACA');
			$('input[name="weight"]').focus();
			return;
		}
		$('input[name="weight"]').css('background','none');
		
		var pattern_name=$('input[name="pattern_name[]"]');
		var min_counts=$('input[name="min_counts[]"]');
		var max_counts=$('input[name="max_counts[]"]');
		var supply_price=$('input[name="supply_price[]"]');
		var delivery_days=$('input[name="delivery_days[]"]');
		
		var shouldCheckOther=true;
		
		for(var i=0;i<pattern_name.size();i++){
			if($(pattern_name.get(i)).val()==''){
				$(pattern_name.get(i)).focus();
				$(pattern_name.get(i)).css('background','#FFCACA');
				shouldCheckOther=false;
				break;
			}
			$(pattern_name.get(i)).css('background','none');
		}

		var shouldCheckCal=true;
		for(var i=0;i<min_counts.size();i++){
			if(shouldCheckOther){
				if($(min_counts.get(i)).val()==''){
					$(min_counts.get(i)).focus();
					$(min_counts.get(i)).css('background','#FFCACA');
					shouldCheckCal=false;
					break;
				}
			}
			$(min_counts.get(i)).css('background','none');
			
			if(shouldCheckOther){
				if($(max_counts.get(i)).val()==''){
					$(max_counts.get(i)).focus();
					$(max_counts.get(i)).css('background','#FFCACA');
					shouldCheckCal=false;
					break;
				}
			}
			$(max_counts.get(i)).css('background','none');
			
			if(shouldCheckOther){
				if($(supply_price.get(i)).val()==''){
					$(supply_price.get(i)).focus();
					$(supply_price.get(i)).css('background','#FFCACA');
					shouldCheckCal=false;
					break;
				}
			}
			$(supply_price.get(i)).css('background','none');
			
			if(shouldCheckOther){
				if($(delivery_days.get(i)).val()==''){
					$(delivery_days.get(i)).focus();
					$(delivery_days.get(i)).css('background','#FFCACA');
					shouldCheckCal=false;
					break;
				}
			}
			$(delivery_days.get(i)).css('background','none');
		}
		
		var shouldContinue=true;
		if(shouldCheckCal&&shouldCheckOther){
			for(var i=0;i<min_counts.size();i++){
				if(parseInt($(min_counts.get(i)).val())>parseInt($(max_counts.get(i)).val())){
					alert("截止采购量不能小于起始采购量!");
					$(max_counts.get(i)).select();
					$(max_counts.get(i)).focus();
					$(max_counts.get(i)).css('background','#FFCACA');
					shouldContinue=false;
					break;
				}
				$(max_counts.get(i)).css('background','none');
				var patternNameSize=$(min_counts.get(i)).parent().prev().find('input[name="pattern_name[]"]').size();
				if(i>0&&patternNameSize==0){
					if(parseInt($(min_counts.get(i)).val())!=parseInt($(max_counts.get(i-1)).val())+1){
						if(parseInt($(min_counts.get(i)).val())>parseInt($(max_counts.get(i-1)).val())+1){
							alert("您的采购量区间之间不连续!");
						}else{
							alert("您的采购量区间之间有重叠!");	
						}
						$(min_counts.get(i)).select();
						$(min_counts.get(i)).focus();
						$(min_counts.get(i)).css('background','#FFCACA');
						shouldContinue=false;
						break;							
					}
					$(min_counts.get(i)).css('background','none');
				}
			}	
		}
		
		if($('#productImages').children().size()==0){
			alert("请上传产品图片!");
			return;			
		}
		var hasSelect=false;
		$('input[name="cut_url"]').each(
			function(){
				if($(this).attr('checked')==true){
					hasSelect=true;
				}
			}
		);
		if(!hasSelect){
			$($('input[name="cut_url"]').get(0)).attr('checked',true);
		}
		
		if(shouldContinue&&shouldCheckOther&&shouldCheckCal){
			/*
			if(trim($('input[name="productyz"]').val())==''){
				alert("请输入验证码!");
				$('input[name="productyz"]').focus();
				$('input[name="productyz"]').css('background','#FFCACA');
				return;
			}*/
			$('input[name="productyz"]').css('background','#ffffff');
			addAllBlank();
			$('#addProductForm').submit();
		}
		
	}
}

function addAllBlank(){
	var patternInputArray=$('input[name="pattern_name[]"]');
	var currentInput=$(patternInputArray.get(0));
	var value=currentInput.val();
	while(true){
		//alert(value);
		if(currentInput.parent().parent().next().find('input[name="min_counts[]"]').size()==0){
			break;
		}
		if(currentInput.parent().parent().next().find('input[name="pattern_name[]"]').size()==0){
			currentInput.parent().parent().next().find('td:first').append('<input type="hidden" name="pattern_name[]" value="'+value+'" />');
			currentInput=currentInput.parent().parent().next().find('input[name="pattern_name[]"]');
		}else{
			patternInputArray=currentInput.parent().parent().next().find('input[name="pattern_name[]"]');
			value=currentInput.parent().parent().next().find('input[name="pattern_name[]"]').val();
			currentInput=currentInput.parent().parent().next().find('input[name="pattern_name[]"]');
		}
	}
}


$(document).ready(
	function(){
		var validate=new validateProduct();
		$('input[name="subject"]').blur(
			function(){
				productCommonCheck.checkProductName(validate,$(this));
				commonEffection.resetFrame('admins_frame',true);
			}
		);
		/*
		$('textarea[name="pattern_intro"]').blur(
			function(){
				productCommonCheck.checkPattern(validate,$(this));
				commonEffection.resetFrame('admins_frame',true);
			}
		);*/

		$('input[name="size1"]').keyup(
			function(){
				productCommonCheck.checkSize(validate,$(this));
			}							  
		);
		$('input[name="size1"]').blur(
			function(){
				productCommonCheck.checkSize(validate,$(this));
			}							  
		);
		
		$('input[name="size2"]').keyup(
			function(){
				productCommonCheck.checkSize(validate,$(this));
			}							  
		);
		$('input[name="size2"]').blur(
			function(){
				productCommonCheck.checkSize(validate,$(this));
			}							  
		);
		
		$('input[name="size3"]').keyup(
			function(){
				productCommonCheck.checkSize(validate,$(this));
			}							  
		);
		$('input[name="size3"]').blur(
			function(){
				productCommonCheck.checkSize(validate,$(this));
			}							  
		);
		
		$('input[name="weight"]').keyup(
			function(){
				productCommonCheck.checkWeight(validate,$(this));
			}							  
		);
		$('input[name="weight"]').blur(
			function(){
				productCommonCheck.checkWeight(validate,$(this));
			}							  
		);
		
		priceCheck(validate);
	}				  
);

window.onload=function(){
	var fck = FCKeditorAPI.GetInstance('FCKeditor1');
	fck.Events.AttachEvent( 'OnBlur', 
		function(){
			productCommonCheck.checkFck(new validateProduct());	
			commonEffection.resetFrame('admins_frame',true);
		}
	);
}
